<?php
 use wepay\join\demo\common\SignUtil;
 use wepay\join\demo\common\DesUtils;
 use wepay\join\demo\common\ConfigUtil;
 use wepay\join\demo\common\RSAUtils;
 use wepay\join\demo\api\WebAsynNotificationCtrl;
/**
 * Class PayController
 *京东支付
 */
class PayController extends BaseController
{  /**
     * 银行直接（网银支付）
     * @return mixed
     */
    public function sends()
    {
        return $this->view('index.sends');
    }
    /**
     * 支付完成返回数据   $this->getParam('username');
     * @return mixed
     */
     public function receive()
    {
        $key='LehurhZRuvtQsSpSWUwmRLmsCrQTfZCp';
        $paramObj  = new stdClass();
        $paramObj->v_oid        =$_POST['v_oid'];    // 支付方式（字符串）   
        $paramObj->v_pmode      =$_POST['v_pmode'];    // 支付方式（字符串）   
        $paramObj->v_pstatus    =$_POST['v_pstatus'];   //  支付状态 ：20（支付成功）；30（支付失败）
        $paramObj->v_pstring    =$_POST['v_pstring'];   // 支付结果信息 ： 支付完成（当v_pstatus=20时）；失败原因（当v_pstatus=30时,字符串）； 
        $paramObj->v_amount     =$_POST['v_amount'];     // 订单实际支付金额
        $paramObj->v_moneytype  =$_POST['v_moneytype']; //订单实际支付币种   
        $paramObj->remark1      =$_POST['remark1' ];      //备注字段1
        $paramObj->v_md5str     =$_POST['v_md5str' ];   //拼凑后的MD5校验值 
        $paramObj->remark2      =$_POST['remark2' ];     //备注字段2
        $md5string=strtoupper(md5($paramObj->v_oid.$paramObj->v_pstatus.$paramObj->v_amount.$paramObj->v_moneytype.$key));
       // print_r($paramObj);
        if ($paramObj->v_md5str==$md5string)
        {
            if($paramObj->v_pstatus=="20")
            {
                // UserService::instance()->Wgecharge($paramObj);

            }
        }
       
    }
    /**
     * 充值成功页面
     */
    public function UserMoneySuccess()
    {
        return $this->view('index.user_money_success');
    }
    /**
     * 异步返回数据
     * @return mixed
     */
    public function autoReceive()
    {
        $key='LehurhZRuvtQsSpSWUwmRLmsCrQTfZCp';
        $paramObj  = new stdClass();
        $paramObj->v_oid        =$this->getParam('v_oid');    // 支付方式（字符串）
        $paramObj->v_pmode      =$this->getParam('v_pmode');    // 支付方式（字符串）
        $paramObj->v_pstatus    =$this->getParam('v_pstatus');   //  支付状态 ：20（支付成功）；30（支付失败）
        $paramObj->v_pstring    =$this->getParam('v_pstring');   // 支付结果信息 ： 支付完成（当v_pstatus=20时）；失败原因（当v_pstatus=30时,字符串）；
        $paramObj->v_amount     =$this->getParam('v_amount');     // 订单实际支付金额
        $paramObj->v_moneytype  =$this->getParam('v_moneytype'); //订单实际支付币种
        $paramObj->remark1      =$this->getParam('remark1');      //备注字段1
        $paramObj->v_md5str     =$this->getParam('v_md5str');   //拼凑后的MD5校验值
        $paramObj->remark2      =$this->getParam('remark2');     //备注字段2
        $paramObj->uid          =substr($paramObj->v_oid , -1);     //备注字段2
        $md5string=strtoupper(md5($paramObj->v_oid.$paramObj->v_pstatus.$paramObj->v_amount.$paramObj->v_moneytype.$key));
        //print_r($paramObj);
        if ($paramObj->v_md5str==$md5string)
        {
            if($paramObj->v_pstatus=="20")
            {
                UserService::instance()->Wgecharge($paramObj);
            }
        }
        return 'ok';
    }


    /**
     * 京东支付pc端
     * @return mixed
     */
     public function jdpay()
    {
        return $this->view('index.jdpay');
    }
    /**
    *支付页面   
    */
    public function  paysign(){
                
        $param = array();
        $param["currency"]              = "CNY";    
        $param["merchantNum"]           = "110206743002";// 22294531
        $param["merchantRemark"]        = "服务平台";
        $param["notifyUrl"]             = "http://www.yangpiaotong.com/checkSign";
        $param["successCallbackUrl"]    = "http://www.yangpiaotong.com/user_money_success";//$_POST["successCallbackUrl"];
        $param["tradeAmount"]           = $_POST["tradeAmount"]*100;
        $param["tradeDescription"]      = $_POST["tradeName"];
        $param["tradeName"]             = $_POST["tradeName"];
        $param["tradeNum"]              = $_POST["tradeNum"].session_get(UserEnum::USER_INFO_USER_ID);;
        $param["ip"]                    = $_POST["ip"]; 
        $param["tradeTime"]             = date('Y-m-d H:i:s',$_POST["tradeTime"]);
        $param["version"]               = "1.1";
        $param["token"]                 = "";
        $specialJson = array();
        $specialJson["specBankCardNo"] =$_POST["specBankCardNo"];
        // $specialJson["specIdCard"] = '371321199207065332';// $_POST["specIdCard"];
        // $specialJson["specName"] = '谢飞';// $_POST["specName"];
        $param["specifyInfoJson"] = json_encode($specialJson);
        $sign = SignUtil::signWithoutToHex($param);
        // print_r($param);
        // die();
        $param["merchantSign"] = $sign;
        $_SESSION['tradeAmount'] = $_POST["tradeAmount"];
        $_SESSION['tradeName'] = $_POST["tradeName"];
        $_SESSION['tradeInfo'] = $param;
        // print_r($_SESSION['tradeInfo']);
        // die();
      
    //跳转到自动提交页
    return $this->view('index.paysubmit');
    }


    /**
     * 异步通知
     */
    public function checkSign() {
        // $testString = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4NCjxDSElOQUJBTks+DQogICAg\n
        //    PFZFUlNJT04+MS4yLjA8L1ZFUlNJT04+DQogICAgPE1FUkNIQU5UPjIyMzEyNzgxPC9NRVJDSEFO\n
        //    VD4NCiAgICA8VEVSTUlOQUw+MDAwMDAwMDE8L1RFUk1JTkFMPg0KICAgIDxEQVRBPkJiQ2NzeVUx\n
        //    L3kyMjlIeXZ4RFQ1Rm1SVnVjSFRXUFJqaGhyWmViRW1wTVAvL2xjdW04cjBRdVhGcjZPeDY5NXdN\n
        //    dEYwblMwWHhjYVYKOVNoMWdrYU16dVJHQXorcytjOWhYREVnMUFXNVNUY3lRM0c3UTZKdzlBajJM\n
        //    V0ZpelQ5cUNRYXVqT1FQT3RPWjIyRWF2RzZzNVJYLwo4c3AzYlJKa3hKNnpDYlQ3ckw0anNJZm9G\n
        //    T05BdDBIV1VYUUpiazRBa3NlK1d3emNybU5QUDVzMVcyckRPUnA5Z3cwcVVhVW9DZmdNCk5LSGNY\n
        //    Ymx2ZVZlVmNZeHlBMHJrRk9xNnFIV0tybEhqRGdqVWl0dFJZQU9CYlA0TDJDSTVvK3dMQzNpV1Z5\n
        //    NmVpTHd2QnNJeWM3amwKU2NhanNaTkgxeDlPbUhUVitXWFA1ejBlejdYb0U1SGJUaDBmckdaeERZ\n
        //    U2wwZlQvMnkvUDFpbnlrVUpwQktiVnA3c2w4NVVyZjVTcgpZZGM0VzA0QXdJajI0NnBpOW1KUHU2\n
        //    d0w2bG5VV24zdXpjT2xDRUxpWkJ6OTJueXI3anlYeXIzR05Ha0VwdFdudXlYenhXV3AzMW8zCmNm\n
        //    MFkwWTMrMGJjbm5BPT08L0RBVEE+DQogICAgPFNJR04+YzhhYTc1NGVmZjQ5MzIyNmYzNzU4NTJk\n
        //    MGFmNTlmMmU8L1NJR04+DQo8L0NISU5BQkFOSz4NCg0K";
        $MD5_KEY="ltsVmgOGkLkrALz3DLYzmmOx0kjyUH1s";
        $DES_KEY="iacI2luo+3anuV4E2r9GGUMW33l1URbW";
        $webAsy = new WebAsynNotificationCtrl();
        $data =$webAsy->execute($MD5_KEY,$DES_KEY,$this->getParam('resp'));
        $xml = simplexml_load_string($data); //创建 SimpleXML对象
        $data = json_decode(json_encode($xml),TRUE);
        // #收集信息

        $paramObj = new stdClass();
        $paramObj->id                   =$data['TRADE']['ID'];
        $paramObj->amount               =$data['TRADE']['AMOUNT']/100;
        $paramObj->time                 =time();
        $paramObj->success              =$data['RETURN']['DESC'];
        $paramObj->uid                  =substr($data['TRADE']['ID'],13,8);
        if($data['RETURN']['CODE']==0000){
            $asd= UserService::instance()->Rechargeinsert($paramObj);
            $extend = UserService::instance()->Extendmoney($paramObj);

        }
        return 'success';

    }






}